package org.jeecg.crawler.task;


import lombok.SneakyThrows;
import org.jeecg.common.system.vo.DictModel;
import org.jeecg.common.util.MD5Util;
import org.jeecg.crawler.other.GettCrawler;
import org.jeecg.crawler.other.SearchNewsCrawler;
import org.jeecg.modules.crawlerpaper.entity.CrawlerInfo;
import org.jeecg.modules.crawlerpaper.service.ICrawlerInfoService;
import org.jeecg.modules.system.service.ISysDictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.util.CollectionUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Configuration      //1.主要用于标记配置类，兼备Component的效果。
@EnableScheduling   // 2.开启定时任务
public class OtherScheduleTask {
    @Autowired
    private ICrawlerInfoService crawlerInfoService;

    @Autowired
    private ISysDictService sysDictService;

    //参数：baseurl,mainUrl,dataurl,page,scoureName,articleTime;

    @SneakyThrows
    @Scheduled(cron = "0 0 1/6 * * ?")
    public void yangshixinwen()  {
        try{
            List<DictModel> keywords = sysDictService.queryDictItemsByCode("keywords");
            if(!CollectionUtils.isEmpty(keywords)){
                List<String> collect = keywords.stream().map(DictModel::getValue).collect(Collectors.toList());

                if(!CollectionUtils.isEmpty(keywords)){
                    for(int i =1;i<= Integer.valueOf(3);i++){

                        for (String keyword : collect) {
                            List<CrawlerInfo>  iCrawlerInfoList = SearchNewsCrawler.getCCTVNewsPageResponse(SearchNewsCrawler.cctvUrl+"&page="+i+"&qtext="+keyword);
                            this.dealList(iCrawlerInfoList);                            }
                        }
                    }





            }
        }catch (Exception e){
            e.printStackTrace();
        }

    }


    @SneakyThrows
    @Scheduled(cron = "0 0 1/6 * * ?")
    public void yangguangwang()  {
        try{
            List<DictModel> keywords = sysDictService.queryDictItemsByCode("keywords");
            if(!CollectionUtils.isEmpty(keywords)){
                List<String> collect = keywords.stream().map(DictModel::getValue).collect(Collectors.toList());
                List<CrawlerInfo> crawlerInfoList = SearchNewsCrawler.getYGWPageResponse( collect, 3,"","");
                this.dealList(crawlerInfoList);
                 }

        }catch (Exception e){
            e.printStackTrace();
        }

    }


    @SneakyThrows
    @Scheduled(cron = "0 0 1/2 * * ?")
    public void guojizaixian()  {
        try{
            List<DictModel> keywords = sysDictService.queryDictItemsByCode("keywords");
            if(!CollectionUtils.isEmpty(keywords)){
                List<String> collect = keywords.stream().map(DictModel::getValue).collect(Collectors.toList());
                List<CrawlerInfo> crawlerInfoList = SearchNewsCrawler.getGJZXageResponse(collect, 3);
                this.dealList(crawlerInfoList);
                 }

        }catch (Exception e){
            e.printStackTrace();
        }



    }
    @SneakyThrows
    @Scheduled(cron = "0 0 1/5 * * ?")
    public void nanJingGuangBoTv()  {
        try{
            List<DictModel> keywords = sysDictService.queryDictItemsByCode("keywords");
            if(!CollectionUtils.isEmpty(keywords)){
                List<String> collect = keywords.stream().map(DictModel::getValue).collect(Collectors.toList());
                List<CrawlerInfo> crawlerInfoList =  SearchNewsCrawler.getNanJingGuangBoNews( collect,3);
                this.dealList(crawlerInfoList);
            }

        }catch (Exception e){
            e.printStackTrace();
        }



    }

    /**
     * 中国江苏网
     */
    @SneakyThrows
    @Scheduled(cron = "0 0 1/5 * * ?")
    public void chinaJingsu()  {
        try{
            List<DictModel> keywords = sysDictService.queryDictItemsByCode("keywords");
            if(!CollectionUtils.isEmpty(keywords)){
                List<String> collect = keywords.stream().map(DictModel::getValue).collect(Collectors.toList());
                List<CrawlerInfo> crawlerInfoList =  SearchNewsCrawler.getChinaJiangsuNews( collect,3);
                this.dealList(crawlerInfoList);
            }

        }catch (Exception e){
            e.printStackTrace();
        }



    }



    /**
     * 紫金山新闻
     */
    @SneakyThrows
    @Scheduled(cron = "0 0 1/5 * * ?")
    public void ziJingShanNews()  {
        try{
            List<DictModel> keywords = sysDictService.queryDictItemsByCode("keywords");
            if(!CollectionUtils.isEmpty(keywords)){
                List<String> collect = keywords.stream().map(DictModel::getValue).collect(Collectors.toList());
                List<CrawlerInfo> crawlerInfoList =  GettCrawler.getZiJingShanNews( collect,3);
                this.dealList(crawlerInfoList);
            }

        }catch (Exception e){
            e.printStackTrace();
        }



    }


    /**
     * 新浪新闻
     */
    @SneakyThrows
    @Scheduled(cron = "0 0 1/5 * * ?")
    public void getSinaNews()  {
        try{
            List<DictModel> keywords = sysDictService.queryDictItemsByCode("keywords");
            if(!CollectionUtils.isEmpty(keywords)){
                List<String> collect = keywords.stream().map(DictModel::getValue).collect(Collectors.toList());
                List<CrawlerInfo> crawlerInfoList =  GettCrawler.getSinaNews( collect,3);
                this.dealList(crawlerInfoList);
            }

        }catch (Exception e){
            e.printStackTrace();
        }



    }





    /**
     * 江南时报
     */
    @SneakyThrows
    @Scheduled(cron = "0 0 1/5 * * ?")
    public void jiangNanShiBao()  {
        try{
            List<DictModel> keywords = sysDictService.queryDictItemsByCode("keywords");
            if(!CollectionUtils.isEmpty(keywords)){
                List<String> collect = keywords.stream().map(DictModel::getValue).collect(Collectors.toList());
                List<CrawlerInfo> crawlerInfoList =  GettCrawler.getJiangNanShiBao( collect,3);
                this.dealList(crawlerInfoList);
            }

        }catch (Exception e){
            e.printStackTrace();
        }



    }



    //  List<CrawlerInfo>  getChinaJiangsuNews(List<String > keywords,Integer page)

    /**
     * 新华业报
     */
    @SneakyThrows
    @Scheduled(cron = "0 0 1/5 * * ?")
    public void xinhuayeBao()  {
        try{
            List<DictModel> keywords = sysDictService.queryDictItemsByCode("keywords");
            if(!CollectionUtils.isEmpty(keywords)){
                List<String> collect = keywords.stream().map(DictModel::getValue).collect(Collectors.toList());
                List<CrawlerInfo> crawlerInfoList =  SearchNewsCrawler.getXinHuaYeBaoNews(collect,3);
                this.dealList(crawlerInfoList);
            }

        }catch (Exception e){
            e.printStackTrace();
        }



    }


    public  void dealList( List<CrawlerInfo> crawlerInfoList) throws ParseException {
        if(!CollectionUtils.isEmpty(crawlerInfoList)){
            Map<String,CrawlerInfo> map = new HashMap<>();

            for (CrawlerInfo crawlerInfo : crawlerInfoList) {
                CrawlerInfo crawlerInfoInMap = map.get(crawlerInfo.getId());
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");

                crawlerInfo.setCreateTime(simpleDateFormat.parse(crawlerInfo.getArticleTime()));
                if(crawlerInfoInMap  ==null){
                    map.put(crawlerInfo.getId(),crawlerInfo);
                }
            }
            crawlerInfoService.saveOrUpdateBatch(new ArrayList<>(map.values()));

        }
    }
    @SneakyThrows
    @Scheduled(cron = "0 0 1/6 * * ?")
    public void xinhuawang()  {
        try{
            List<DictModel> keywords = sysDictService.queryDictItemsByCode("keywords");
            if(!CollectionUtils.isEmpty(keywords)){
                List<String> collect = keywords.stream().map(DictModel::getValue).collect(Collectors.toList());
                List<CrawlerInfo> crawlerInfoList = SearchNewsCrawler.getXinhuaResponse(collect, Integer.valueOf(3));
                if(!CollectionUtils.isEmpty(crawlerInfoList)){
                    Map<String,CrawlerInfo> map = new HashMap<>();

                    for (CrawlerInfo crawlerInfo : crawlerInfoList) {
                        CrawlerInfo crawlerInfoInMap = map.get(crawlerInfo.getArticleName());
                        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
                        crawlerInfo.setId(MD5Util.MD5Encode(crawlerInfo.getArticleName()+crawlerInfo.getArticleTime(),"utf-8"));
                        crawlerInfo.setCreateTime(simpleDateFormat.parse(crawlerInfo.getArticleTime()));
                        if(crawlerInfoInMap  ==null){
                            map.put(crawlerInfo.getArticleName(),crawlerInfo);
                        }
                    }
                    crawlerInfoService.saveOrUpdateBatch(new ArrayList<>(map.values()));

                }
            }

        }catch (Exception e){
            e.printStackTrace();
        }


    }

    @SneakyThrows
    @Scheduled(cron = "0 0 1/6 * * ?")
    public void xuexiqiangguo()  {
        try{
            List<DictModel> keywords = sysDictService.queryDictItemsByCode("keywords");
            if(!CollectionUtils.isEmpty(keywords)){
                List<String> collect = keywords.stream().map(DictModel::getValue).collect(Collectors.toList());
                List<CrawlerInfo> crawlerInfoList = SearchNewsCrawler.getXXQGNews(collect,3);
                dealList(  crawlerInfoList);

            }
        }catch (Exception e){
            e.printStackTrace();
        }



    }

    @SneakyThrows
    @Scheduled(cron = "0 0 1/6 * * ?")
    public void zhongguoribao()  {
        try{
            List<DictModel> keywords = sysDictService.queryDictItemsByCode("keywords");
            if(!CollectionUtils.isEmpty(keywords)){
                List<String> collect = keywords.stream().map(DictModel::getValue).collect(Collectors.toList());
                List<CrawlerInfo> crawlerInfoList = SearchNewsCrawler.getZGRBPageResponse(collect, 3);
                dealList(  crawlerInfoList);
            }
        }catch (Exception e){
            e.printStackTrace();
        }



    }




}
